/**
  **************************************************************************
  * @file     at32f421_board.h
  * @brief    header file for at-start board. set of firmware functions to
  *           manage leds and push-button. initialize delay function.
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#ifndef __FLDRV_BOARD_H
#define __FLDRV_BOARD_H

#ifdef __cplusplus
extern "C" {
#endif

#include "stdio.h"
#include "at32f421.h"
#include "at32f421_gpio.h"
#if !defined (AT_START_F421_V1)
#error "please select first the board at-start device used in your application (in at32f421_board.h file)"
#endif
 
#define	RD_CHARGE_STD		  gpio_input_data_bit_read(GPIOA,GPIO_PINS_0)	//充电满
#define	RD_VUSB					  gpio_input_data_bit_read(GPIOA,GPIO_PINS_6)	//USB插入
#define	RD_VISIT_IN			  gpio_input_data_bit_read(GPIOA,GPIO_PINS_11)//随访
#define	RD_POW_KEY			  gpio_input_data_bit_read(GPIOB,GPIO_PINS_0)	//电源
#define	RD_HALL_IN			  gpio_input_data_bit_read(GPIOB,GPIO_PINS_4)	//霍尔
#define	RD_VOICE_BUSY		  gpio_input_data_bit_read(GPIOB,GPIO_PINS_10)	//忙检测
#define	RD_VOL_ADD			  gpio_input_data_bit_read(GPIOB,GPIO_PINS_14) //音量+
#define	RD_VOL_SUB			  gpio_input_data_bit_read(GPIOB,GPIO_PINS_15) //音量-


#define	EN_SYS_POW_ON			gpio_bits_write(GPIOA,GPIO_PINS_5,TRUE)	//系统电源
#define	EN_SYS_POW_OFF		gpio_bits_write(GPIOA,GPIO_PINS_5,FALSE)
#define	EN_MO_POW_ON			gpio_bits_write(GPIOA,GPIO_PINS_7,TRUE)	//模组电源
#define	EN_MO_POW_OFF			gpio_bits_write(GPIOA,GPIO_PINS_7,FALSE)
#define	EN_CHAR_ASSI_OFF	gpio_bits_write(GPIOB,GPIO_PINS_11,FALSE)	//辅助充电
#define	EN_CHAR_ASSI_ON		gpio_bits_write(GPIOB,GPIO_PINS_11,TRUE)
#define	EN_VOICE_DAT_L		gpio_bits_write(GPIOB,GPIO_PINS_2,FALSE)	//音频数据
#define	EN_VOICE_DAT_H		gpio_bits_write(GPIOB,GPIO_PINS_2,TRUE)

#define	LED_NET_BAD_ON		gpio_bits_write(GPIOA,GPIO_PINS_12,FALSE)	//网差
#define	LED_NET_BAD_OFF		gpio_bits_write(GPIOA,GPIO_PINS_12,TRUE)
#define	LED_SUB_VIS_OFF		gpio_bits_write(GPIOB,GPIO_PINS_6,FALSE)	//复诊灯
#define	LED_SUB_VIS_ON		gpio_bits_write(GPIOB,GPIO_PINS_6,TRUE)
#define	LED_LOW_POW_OFF		gpio_bits_write(GPIOB,GPIO_PINS_7,FALSE)	//低电充电灯
#define	LED_LOW_POW_ON		gpio_bits_write(GPIOB,GPIO_PINS_7,TRUE)
#define	LED_MED_ALA_OFF		gpio_bits_write(GPIOB,GPIO_PINS_8,FALSE)	//服药灯
#define	LED_MED_ALA_ON		gpio_bits_write(GPIOB,GPIO_PINS_8,TRUE)
#define	LED_CHAR_FULL_OFF	gpio_bits_write(GPIOB,GPIO_PINS_9,FALSE)	//充电满
#define	LED_CHAR_FULL_ON	gpio_bits_write(GPIOB,GPIO_PINS_9,TRUE)
#define	LED_NET_OK_ON			gpio_bits_write(GPIOF,GPIO_PINS_6,FALSE)	//网好
#define	LED_NET_OK_OFF		gpio_bits_write(GPIOF,GPIO_PINS_6,TRUE)

/******************** define led ********************/
typedef enum
{
  LED2                                   = 0,
  LED3                                   = 1,
  LED4                                   = 2,
	EN_MO_POW                                 = 3
} led_type;

#define LED_NUM                          4

#if defined (AT_START_F421_V1)
#define LED2_PIN                         GPIO_PINS_6
#define LED2_GPIO                        GPIOB
#define LED2_GPIO_CRM_CLK                CRM_GPIOB_PERIPH_CLOCK

#define LED3_PIN                         GPIO_PINS_7
#define LED3_GPIO                        GPIOB
#define LED3_GPIO_CRM_CLK                CRM_GPIOB_PERIPH_CLOCK

#define LED4_PIN                         GPIO_PINS_8
#define LED4_GPIO                        GPIOB
#define LED4_GPIO_CRM_CLK                CRM_GPIOB_PERIPH_CLOCK

#define MO_POW_PIN                       GPIO_PINS_7
#define MO_POW_GPIO                      GPIOA
#define MO_POW_GPIO_CRM_CLK              CRM_GPIOA_PERIPH_CLOCK
#endif

/**************** define print uart ******************/
#define PRINT_UART                       USART1
#define PRINT_UART_CRM_CLK               CRM_USART1_PERIPH_CLOCK
#define PRINT_UART_TX_PIN                GPIO_PINS_9
#define PRINT_UART_TX_GPIO               GPIOA
#define PRINT_UART_TX_GPIO_CRM_CLK       CRM_GPIOA_PERIPH_CLOCK
#define PRINT_UART_TX_PIN_SOURCE         GPIO_PINS_SOURCE9
#define PRINT_UART_TX_PIN_MUX_NUM        GPIO_MUX_1

/******************* define button *******************/
typedef enum
{
  USER_BUTTON                            = 0,
  NO_BUTTON                              = 1
} button_type;

#define USER_BUTTON_PIN                  GPIO_PINS_0
#define USER_BUTTON_PORT                 GPIOA
#define USER_BUTTON_CRM_CLK              CRM_GPIOA_PERIPH_CLOCK

/**
  * @}
  */

/** @defgroup BOARD_exported_functions
  * @{
  */

/******************** functions ********************/
void at32_board_init(void);

/* led operation function */
void at32_led_init(led_type led);
void at32_led_on(led_type led);
void at32_led_off(led_type led);
void at32_led_toggle(led_type led);

/* button operation function */
void at32_button_init(void);
button_type at32_button_press(void);
uint8_t at32_button_state(void);

/* delay function */
void delay_init(void);
void delay_us(uint32_t nus);
void delay_ms(uint16_t nms);
void delay_sec(uint16_t sec);

/* printf uart init function */
void uart_print_init(uint32_t baudrate);
void at32_board_init(void);
void fldrv_board_init(void);
/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif

